package org.paradise.dms.web.action.systemusrmgr;

import java.util.List;

import javax.servlet.http.HttpSession;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.log4j.Logger;
import org.directwebremoting.annotations.RemoteProxy;
import org.paradise.dms.pojo.SystemUser;
import org.paradise.dms.pojo.SystemUserGroup;
import org.paradise.dms.pojo.SystemUserGroupRole;
import org.paradise.dms.services.AuthenticateService;
import org.paradise.dms.services.SystemUserGroupMgrService;
import org.paradise.dms.services.SystemUserMgrService;
import org.paradise.dms.web.action.DMSBaseAction;
import org.paradise.dms.web.tools.DWRPage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import com.dheaven.framework.dao.DaoException;

/**
 * 
 * Description: 系统用户以及用户组的CRUD管理Action
 * 
 * Copyright (c) 2008-2009 paraDise sTudio(DT). All Rights Reserved.
 * 
 * @version 1.0 2009-3-17 上午12:05:56 李双江（paradise.lsj@gmail.com）created
 */
@Service
@RemoteProxy(name = "systemUserMgrAction")
public class SystemUserMgrAction extends DMSBaseAction {

	/**
	 * 
	 */
	private static final long serialVersionUID = -3438968354104415561L;
	private static Logger log = Logger.getLogger(SystemUserMgrAction.class);
	@Autowired
	private SystemUserMgrService systemUserMgrService;
	@Autowired
	@Qualifier("systemUserGroupMgrServiceImpl")
	private SystemUserGroupMgrService systemUserGroupMgrService;

	@Autowired
	private AuthenticateService authenticateService;

	public AuthenticateService getAuthenticateService() {
		return authenticateService;
	}

	public void setAuthenticateService(AuthenticateService authenticateService) {
		this.authenticateService = authenticateService;
	}

	public SystemUserGroupMgrService getSystemUserGroupMgrService() {
		return systemUserGroupMgrService;
	}

	public void setSystemUserGroupMgrService(SystemUserGroupMgrService systemUserGroupMgrService) {
		this.systemUserGroupMgrService = systemUserGroupMgrService;
	}

	public List<SystemUserGroup> systemUserGroupInfoList;

	/**
	 * 
	 * Description: 取到系统所有的用户组名<增加系统用户的select框中需要>
	 * 
	 * @Version1.0 2009-4-2 上午01:12:10 李双江（paradise.lsj@gmail.com）创建
	 * @return
	 */
	public JSONArray getSystemUserGroupInfo() {
		systemUserGroupInfoList = systemUserMgrService.getSystemUserGroupInfo();
		if (systemUserGroupInfoList != null & systemUserGroupInfoList.size() > 0) {
			JSONArray jsonArray = JSONArray.fromObject(systemUserGroupInfoList);
			return jsonArray;
		}
		return null;
	}

	public List<SystemUserGroup> getSystemUserGroupInfoList() {
		return systemUserGroupInfoList;
	}

	public void setSystemUserGroupInfoList(List<SystemUserGroup> systemUserGroupInfoList) {
		this.systemUserGroupInfoList = systemUserGroupInfoList;
	}

	/*
	 * 
	 * Description:增加一条系统用户记录
	 * 
	 * @Version1.0 2009-3-17 上午12:14:25 李双江（paradise.lsj@gmail.com）创建
	 * 
	 * @return
	 */
	public String addSystemUser(String systemusername, String systemusersex, String systemusernickname, String systemuserpassword, String systemusergroupid, String systemuseridentitycardno, String systemuserbirthday, String systemuserbirthplace, String systemusertelephone, String systemuseremail, String systemuseraddress) {
		// 判断系统用户名是否有重复
		if (systemUserMgrService.isSystemUserNameExists(systemusername)) {
			log.warn("DMS_warn:创建一个已经存在的用户名 " + systemusername + " 失败！");
			return "系统用户名已经存在！";
		} else {
			SystemUser su = new SystemUser();
			su.setSystemusername(systemusername);
			su.setSystemusersex(systemusersex);
			su.setSystemuserind("1"); // 默认用户是可用的
			su.setSystemusernickname(systemusernickname);
			su.setSystemuserpassword(systemuserpassword);
			su.setSystemusergroupid(systemusergroupid);
			su.setSystemuseridentitycardno(systemuseridentitycardno);
			su.setSystemuserbirthday(systemuserbirthday);
			su.setSystemuserbirthplace(systemuserbirthplace);
			su.setSystemusertelephone(systemusertelephone);
			su.setSystemuseremail(systemuseremail);
			su.setSystemuseraddress(systemuseraddress);
			boolean flag = systemUserMgrService.insertSystemUser(su);
			if (flag) {
				log.info("DMS_info:插入系统用户成功!");
				return "添加系统用户成功!";
			}
			return "系统忙,请稍候再试...";
		}
	}

	/**
	 * 
	 * Description: 按照用户组id列出改组的所有用户
	 * 
	 * @Version1.0 2009-4-2 下午09:55:29 李双江（paradise.lsj@gmail.com）创建
	 * @param systemusergroupid
	 * @return
	 */
	public JSONArray listSystemUserPerPageByGroupID(int pageNum, int totalRow, String systemusergroupid, HttpSession session) {
		DWRPage page = new DWRPage();
		page.setPageProperties(totalRow, 12);
		page.setCurrentPage(pageNum);
		session.setAttribute("page", page);
		int cp = (page.getCurrentPage() - 1) * 12;
		int ps = page.getPageSize();
		List<SystemUser> sulist = systemUserMgrService.getSystemUserByGroupID(systemusergroupid, ps, cp);
		if (sulist != null & sulist.size() > 0) {
			JSONArray jsonArray = JSONArray.fromObject(sulist);
			return jsonArray;
		}
		return null;
	}

	/**
	 * 
	 * Description:返回DWR页面分页对象
	 * 
	 * @Version1.0 2009-3-28 上午11:13:26 李双江（paradise.lsj@gmail.com）创建
	 * @param session
	 * @return
	 */
	@SuppressWarnings("static-access")
	public JSONObject getPage(HttpSession session) {
		JSONObject joa = new JSONObject().fromObject((DWRPage) session.getAttribute("page"));
		log.info(joa);
		return joa;
	}

	/**
	 * 
	 * Description: 根据systemuserid去删除一条用户
	 * 
	 * @Version1.0 2009-4-2 下午11:17:55 李双江（paradise.lsj@gmail.com）创建
	 * @param systemuserid
	 * @return
	 */
	public String delelteSystemUserByID(String systemuserid) {
		return systemUserMgrService.delelteSystemUserByID(systemuserid);
	}

	/**
	 * 
	 * Description: 插入一条用户组记录，同时返回该用户组的JSONArray对象
	 * 
	 * @Version1.0 2009-4-4 下午05:50:21 李双江（paradise.lsj@gmail.com）创建
	 * @param systemusergroupname
	 * @param systemusergroupvalue
	 * @return
	 */
	public JSONArray insertSystemUserGroup(String systemusergroupname, String systemusergroupvalue) {
		// 根据systemusergroupname去查是否有重复组
		try {
			List<SystemUserGroup> lst = systemUserGroupMgrService.getSystemUserGroupByName(systemusergroupname);
			if (lst != null && lst.size() > 0) {
				JSONArray jsona = new JSONArray();
				jsona.add("系统用户组已经存在!");
				jsona.add("请重新输入"); // trick
				log.info(jsona);
				return jsona;
			} else { // 不存在，可以插入
				SystemUserGroup sug = new SystemUserGroup();
				sug.setSystemusergroupind("1");
				sug.setSystemusergroupname(systemusergroupname);
				sug.setSystemusergroupvalue(Integer.parseInt(systemusergroupvalue));
				sug.setSystemuserpermissionid("-1"); // 先把perssionid设置为-1，后面要自己选择权限
				List<SystemUserGroup> inList = systemUserGroupMgrService.insertSystemUserGroup(sug);
				if (inList != null && inList.size() > 0) { // 插入成功
					JSONArray jsonArray = JSONArray.fromObject(inList);
					return jsonArray;
				} else {
					return null;
				}
			}
		} catch (DaoException e) {
			log.error("DMS_error:根据插入系统用户组有误！");
			log.error("DMS_error:错误原因！" + e);
		}
		return null;
	}

	/**
	 * 
	 * Description: 插入一条系统用户权限值，返回插入状态
	 * 
	 * @Version1.0 2009-4-4 下午07:01:00 李双江（paradise.lsj@gmail.com）创建
	 * @param systemusergroupid
	 * @param systemuserpermissionvalue
	 * @return
	 */
	public String insertSystemUserPermission(String systemusergroupid, String systemuserpermissionvalue) {
		SystemUserGroupRole sugr = new SystemUserGroupRole();
		sugr.setSystemusergroupid(systemusergroupid);
		sugr.setSystemuserpermissionvalue(systemuserpermissionvalue);
		return systemUserMgrService.insertSystemUserPermission(sugr);
	}

	public String updateSystemUserPermission(String systemusergroupid, String systemuserpermissionvalue) {
		return systemUserMgrService.updateSystemUserPermission(systemusergroupid, systemuserpermissionvalue);
	}

	/**
	 * 
	 * Description:按照用户组id列出改组的用户总行数
	 * 
	 * @Version1.0 2009-4-2 下午10:44:39 李双江（paradise.lsj@gmail.com）创建
	 * @param systemusergroupid
	 * @return
	 * @throws DaoException
	 */
	public int getAGroupIDSysUserTotalRows(String systemusergroupid) throws DaoException {
		return systemUserMgrService.getAGroupIDSysUserTotalRows(systemusergroupid);
	}

	/**
	 * 
	 * Description: 根据用户组id去查用户的权限值
	 * 
	 * @Version1.0 2009-4-4 下午05:29:35 李双江（paradise.lsj@gmail.com）创建
	 * @param systemusergroupid
	 * @return
	 */
	@SuppressWarnings("static-access")
	public JSONObject listSystemUserGroupRoleValueByGroupID(String systemusergroupid) {
		List<SystemUserGroupRole> lst = authenticateService.getSystemUserPermissionValue(systemusergroupid);
		if (lst != null && lst.size() > 0) {
			String roleString = lst.get(0).getSystemuserpermissionvalue();
			JSONObject upv = new JSONObject().fromObject(roleString);
			return upv;
		}
		return null;
	}

	/**
	 * 
	 * Description:查询系统用户组总行数
	 * 
	 * @Version1.0 Apr 21, 2009 2:22:26 AM 李双江（paradise.lsj@gmail.com）创建
	 * @return
	 */
	public int getSystemUserGroupTotalRows() {
		return systemUserMgrService.getSystemUserGroupTotalRows();
	}

	/**
	 * 
	 * Description: 分页查询系统用户组
	 * 
	 * @Version1.0 Apr 21, 2009 2:30:12 AM 李双江（paradise.lsj@gmail.com）创建
	 * @param pageNum
	 * @param totalRow
	 * @param session
	 * @return
	 */
	public JSONArray listAllSysUserGroupListByPage(int pageNum, int totalRow, HttpSession session) {
		DWRPage page = new DWRPage();
		page.setPageProperties(totalRow, 5);
		page.setCurrentPage(pageNum);
		session.setAttribute("page", page);
		int cp = (page.getCurrentPage() - 1) * 5;
		int ps = page.getPageSize();
		List<SystemUserGroup> lst = systemUserMgrService.listAllSysUserGroupListByPage(ps, cp);
		if (lst != null & lst.size() > 0) {
			JSONArray ja = JSONArray.fromObject(lst);
			return ja;
		}
		return null;
	}

	/**
	 * 
	 * Description: 根据systemusergroupid去删除一个系统用户组
	 * 
	 * @Version1.0 Apr 21, 2009 2:35:59 AM 李双江（paradise.lsj@gmail.com）创建
	 * @param systemusergroupid
	 * @return
	 */
	public String deleteSysUserGroupById(String systemusergroupid) {
		return systemUserMgrService.deleteSysUserGroupById(systemusergroupid);
	}

	/**
	 * 
	 * Description:更新一条系统用户信息
	 * 
	 * @Version1.0 Apr 26, 2009 1:51:21 AM 李双江（paradise.lsj@gmail.com）创建
	 * @param sysuserStr
	 * @return
	 */
	public String updateSystemUser(String sysuserStr) {
		// systemuserid:null, systemuserind:null,
		// systemuserpassword:null,systemuseremail:null,systemuserbirthday:null,
		// systemuserbirthplace:null,systemuseridentitycardno:null,systemuseraddress:null,systemusername:null,
		// systemusernickname:null,systemusersex:null,systemtelephone:null,systemusergroupid:null
		SystemUser su = new SystemUser();
		String[] sustr = sysuserStr.split(",");
		su.setSystemuserid(sustr[0]);
		su.setSystemuserind(sustr[1]);
		su.setSystemuserpassword(sustr[2]);
		su.setSystemuseremail(sustr[3]);
		if (sustr[4] == "") {
			su.setSystemuserbirthday(null); // Birthday数据库为date类型，直接赋String会出错
		}
		su.setSystemuserbirthplace(sustr[5]);
		su.setSystemuseridentitycardno(sustr[6]);
		su.setSystemuseraddress(sustr[7]);
		su.setSystemusername(sustr[8]);
		su.setSystemusernickname(sustr[9]);
		su.setSystemusersex(sustr[10]);
		su.setSystemusertelephone(sustr[11]);
		su.setSystemusergroupid(sustr[12]);
		return systemUserMgrService.updateSystemUser(su);
	}

	public void setSystemUserMgrService(SystemUserMgrService systemUserMgrService) {
		this.systemUserMgrService = systemUserMgrService;
	}

	public SystemUserMgrService getSystemUserMgrService() {
		return systemUserMgrService;
	}
}
